package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Wang
 * @version 1.0.0
 * @createTime 2020年08月25日 11:12:00
 * @Description: Role 业务实现类
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    /**
     * 分页查询角色
     */
    @Autowired
    private RoleDao roleDao;
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.info("[查询]data:{}", queryPageBean);
        //调用PageHelper插件完成查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Role> page = roleDao.selectByPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加角色和关联的信息
     * @param role
     * @param menus
     * @param permissions
     */
    @Transactional
    @Override
    public void add(Role role, List<Integer> menus,List<Integer> permissions) {
        //插入到t_role表
        roleDao.insert(role);
        log.info("[role]data:{}",role.getId());
        //插入到t_role_menu
        if (menus.size() != 0) {
            roleDao.insertMenuByRoleId(menus,role.getId());
        }
        log.info("[menus]data:{}",menus);
        //插入到t_role_permission
        if (menus.size() != 0) {
            roleDao.insertPermissionByRoleId(permissions,role.getId());
        }
        log.info("[permissions]data:{}",permissions);
    }

    /**
     * 根据id查询角色信息
     * @param id
     * @return
     */
    @Override
    public Role findById(Integer id) {
        Role role = roleDao.selectById(id);
        return role;
    }

    /**
     * 根据roleId 查询menuIds
     * @param id
     * @return
     */
    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        List<Integer> menuIds = roleDao.selectMenuIdsByRoleId(id);
        return menuIds;
    }

    /**
     * 根据roleId查询permissionIds
     * @param id
     * @return
     */
    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        List<Integer> permissionIds = roleDao.selectPermissionIdsByRoleId(id);
        return permissionIds;
    }

    @Transactional
    @Override
    public void edit(Role role, List<Integer> menuIds, List<Integer> permissionIds) {
        /**
         * 因为要一起更新关联表中的数据
         * 所以要先删除关联表中的数据
         * 再把前端带回的数据增加进去
         */
        roleDao.updateRole(role);
        roleDao.deleteMenuIdsByRoleId(role.getId());
        roleDao.deletePermissionIdsByRoleId(role.getId());
        if (menuIds.size() != 0) {
            roleDao.insertMenuByRoleId(menuIds,role.getId());
        }
        if (permissionIds.size() != 0) {
            roleDao.insertPermissionByRoleId(permissionIds,role.getId());
        }
    }

    /**
     * 根据roleId 删除
     * @param id
     */
    @Transactional
    @Override
    public void delete(Integer id) {
        /**
         * 删除之前要先判断是否与user关联
         * 然后再根据roleId
         * 删除t_role_menu和t_role_permission
         * 表中的数据
         */
        Long count = roleDao.selectUserIdByRoleId(id);
        if (count > 0) {
            throw new BusinessRuntimeException("此条数据与其他项有关联，不能删除！！！");
        }
        roleDao.deleteMenuIdsByRoleId(id);
        roleDao.deletePermissionIdsByRoleId(id);
        roleDao.deleteRoleById(id);
    }
}
